package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeIntakeAirPressure;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeIntakeAirPressureEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 发动机进气压力区间算法<br>
 * 输出：统计<br>
 * @author 沈东生
 */
public class RangeIntakeAirPressure extends AbstractArithmetic<VarRangeIntakeAirPressure> {
    @Override
    public void calculate() {
        logger.debug("进入发动机进气压力区间算法.");
        CommonData data = getCommonData();
        Integer intakeAirPressure = data.getIntakePressure();

        if (intakeAirPressure == null) {
            return;
        }
        if (!RangeIntakeAirPressureEnum.RANGE_VALID.getRange().contains(intakeAirPressure)) {
            return;
        }

        int duration = data.getDffTime();
        VarRangeIntakeAirPressure var = getObj();
        if (RangeIntakeAirPressureEnum.RANGE_50.getRange().contains(intakeAirPressure)) {
            var.setIp50Duration( Optional.ofNullable(var.getIp50Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_60.getRange().contains(intakeAirPressure)) {
            var.setIp60Duration(  Optional.ofNullable(var.getIp60Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_70.getRange().contains(intakeAirPressure)) {
            var.setIp70Duration( Optional.ofNullable(var.getIp70Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_80.getRange().contains(intakeAirPressure)) {
            var.setIp80Duration( Optional.ofNullable(var.getIp80Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_90.getRange().contains(intakeAirPressure)) {
            var.setIp90Duration( Optional.ofNullable(var.getIp90Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_100.getRange().contains(intakeAirPressure)) {
            var.setIp100Duration( Optional.ofNullable(var.getIp100Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_110.getRange().contains(intakeAirPressure)) {
            var.setIp110Duration(  Optional.ofNullable(var.getIp110Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_120.getRange().contains(intakeAirPressure)) {
            var.setIp120Duration( Optional.ofNullable(var.getIp120Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_130.getRange().contains(intakeAirPressure)) {
            var.setIp130Duration(  Optional.ofNullable(var.getIp130Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_140.getRange().contains(intakeAirPressure)) {
            var.setIp140Duration( Optional.ofNullable(var.getIp140Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_150.getRange().contains(intakeAirPressure)) {
            var.setIp150Duration( Optional.ofNullable(var.getIp150Duration()).orElse(0) + duration);
        } else if (RangeIntakeAirPressureEnum.RANGE_OVER_150.getRange().contains(intakeAirPressure)) {
            var.setIpOver150Duration( Optional.ofNullable(var.getIpOver150Duration()).orElse(0) + duration);
        }

        if (var.getIpMax() == null || intakeAirPressure > var.getIpMax()) {
            var.setIpMax(intakeAirPressure);
        }
        if (var.getIpMin() == null || intakeAirPressure < var.getIpMin()) {
            var.setIpMin(intakeAirPressure);
        }

        var.setIpSum(Optional.ofNullable(var.getIpSum()).orElse(0L) + intakeAirPressure);
        var.setIpCount(Optional.ofNullable(var.getIpCount()).orElse(0) + 1);
        logger.debug("离开发动机进气压力区间算法.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeIntakeAirPressure var = getObj();
        statisticDataOut.setIp50Duration(ArithmeticUtils.timeMsToSecond(var.getIp50Duration()));
        statisticDataOut.setIp60Duration(ArithmeticUtils.timeMsToSecond(var.getIp60Duration()));
        statisticDataOut.setIp70Duration(ArithmeticUtils.timeMsToSecond(var.getIp70Duration()));
        statisticDataOut.setIp80Duration(ArithmeticUtils.timeMsToSecond(var.getIp80Duration()));
        statisticDataOut.setIp90Duration(ArithmeticUtils.timeMsToSecond(var.getIp90Duration()));
        statisticDataOut.setIp100Duration(ArithmeticUtils.timeMsToSecond(var.getIp100Duration()));
        statisticDataOut.setIp110Duration(ArithmeticUtils.timeMsToSecond(var.getIp110Duration()));
        statisticDataOut.setIp120Duration(ArithmeticUtils.timeMsToSecond(var.getIp120Duration()));
        statisticDataOut.setIp130Duration(ArithmeticUtils.timeMsToSecond(var.getIp130Duration()));
        statisticDataOut.setIp140Duration(ArithmeticUtils.timeMsToSecond(var.getIp140Duration()));
        statisticDataOut.setIp150Duration(ArithmeticUtils.timeMsToSecond(var.getIp150Duration()));
        statisticDataOut.setIpOver150Duration(ArithmeticUtils.timeMsToSecond(var.getIpOver150Duration()));

        statisticDataOut.setIpSum(var.getIpSum());
        statisticDataOut.setIpCount(var.getIpCount());
        statisticDataOut.setMaxIp(var.getIpMax());
        statisticDataOut.setMinIp(var.getIpMin());
    }
}
